<?php
/**
 * Copyright (C) Alibaba Cloud Computing
 * All rights reserved
 */

require_once realpath(dirname(__FILE__) . '/Request.php');

/**
 * The request used to create logstore from log service.
 *
 * @author log service dev
 */
class Aliyun_Log_Models_CreateLogstoreIndexRequest extends Aliyun_Log_Models_Request
{
    private $logstore;
    private $keys;
    private $line;

    /**
     * Aliyun_Log_Models_CreateLogstoreRequest constructor
     *
     * @param string $project project name
     */
    public function __construct($project = null, $logstore = null)
    {
        parent::__construct($project);
        $this->logstore = $logstore;
        $this->keys     = [];
        $this->line     = [];
    }

    /**
     * 创建索引
     * @param  string   $key                索引字段
     * @param  string   $type               字段类型 text、long、double
     * @param  boolean  $chn                是否包含中文，只有type为text时才有意义
     * @param  boolean  $case_sensitive     是否大小写敏感，只有type为text时有意义
     * @param  boolean  $doc_value          该字段是否开启统计
     * @return boolean
     */
    public function addKey($key, $type, $chn = false, $case_sensitive = false, $doc_value = true)
    {
        $data = [
            "type"      => $type,
            "doc_value" => $doc_value,
        ];
        if ($type === 'text') {
            $data['token']         = [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"];
            $data['chn']           = $chn;
            $data['caseSensitive'] = $case_sensitive;
        }
        $this->keys[$key] = $data;
    }

    public function openLine()
    {
        $this->line = [
            'caseSensitive' => false,
            'chn' => true,
            'token' => [",", " ", "'", "\"", ";", "=", "(", ")", "[", "]", "{", "}", "?", "@", "&", "<", ">", "/", ":", "\n", "\t", "\r"],
        ];
    }

    public function getLogstore()
    {
        return $this->logstore;
    }

    public function createBody()
    {
        $body = [];

        if (!empty($this->line)) {
            $body['line'] = $this->line;
        }
        if (!empty($this->keys)) {
            $body['keys'] = $this->keys;
        }

        return $body;
    }
}
